/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pokemon.services;

import com.pokemon.dao.TrainerDAO;
import com.pokemon.dto.DTOConvertor;
import com.pokemon.dto.TrainerDTO;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Branislav Paulis
 */
@Service("trainerManager")
@Transactional
public class TrainerServiceImpl implements TrainerService {

    @Autowired
    private TrainerDAO trainerDAO;
   
    @Required
    @Autowired
    public void setTrainerDAO(TrainerDAO trainerDAO)
    {
        this.trainerDAO = trainerDAO;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional
    public TrainerDTO addTrainer(TrainerDTO trainer) {
        trainerDAO.addTrainer(DTOConvertor.dtoToEntity(trainer));
        return trainer;
    }

    @Transactional
    public TrainerDTO getTrainer(Long id) {
        return DTOConvertor.entityToDto(trainerDAO.getTrainer(id));
    }
   
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional
    public TrainerDTO updateTrainer(TrainerDTO trainer) {
        trainerDAO.updateTrainer(DTOConvertor.dtoToEntity(trainer));
        return trainer;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional
    public TrainerDTO removeTrainer(TrainerDTO trainer) {
        trainerDAO.removeTrainer(DTOConvertor.dtoToEntity(trainer));
        return trainer;
    }

    @Transactional
    public List<TrainerDTO> getAllTrainers() {
        return DTOConvertor.trainerEntityListToDtoList(trainerDAO.getAllTrainers());
    }
}
